﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using System.Web;
using System.Web.Security;
using System.Reflection;
using System.Data;
using System.Configuration;
using Castle.Core;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
//using ExtAspNet;using BJAPP.Web.core;
//using System.Web.UI.HtmlControls;
//using BJAPP.Domain;
//using BJAPP.Service;
//using BJAPP.domain;


namespace Web.core
{
    public class PageBase : System.Web.UI.Page
    {
        //public string SYS_USERS_SESSION_KEY = "BJAPP_SYS_USERS_KEY";
        protected BindingFlags BINDING_FLAGS_SET
                    = BindingFlags.Public
                    | BindingFlags.SetProperty
                    | BindingFlags.Instance
                    | BindingFlags.SetField
                    ;
       
        protected override void OnInit(EventArgs e)
        {
            IWindsorContainer container = ObtainContainer();

            Type type = this.GetType();

            PropertyInfo[] properties = type.GetProperties(BINDING_FLAGS_SET);

            foreach (PropertyInfo propertie in properties)
            {
                string pname = propertie.Name;

                if (container.Kernel.HasComponent(pname))
                {
                    propertie.SetValue(this, container[pname], null);
                }
            }
            //extNet();
            //SetCssAndJsLink();
            //checkRole();
            base.OnInit(e);

            
           
        }

        #region 设置连接样式和js文件
        /// <summary>
        /// 设置连接样式和js文件
        /// 	<script type="text/javascript" src="ymPrompt.js"></script>
	     ///   <link rel="stylesheet" id='skin' type="text/css" href="skin/qq/ymPrompt.css" />
        /// </summary>
        //private void SetCssAndJsLink()
        //{
        //    HtmlLink link1 = new HtmlLink();
        //    link1.Attributes.Add("type", "text/css");
        //    link1.Attributes.Add("rel", "stylesheet");
        //    link1.Attributes.Add("href", "~/common/ymPrompt-4.0-B-20090302/skin/qq/ymPrompt.css");

        //    HtmlGenericControl scriptControl2 = new HtmlGenericControl("script");
        //    scriptControl2.Attributes.Add("type", "text/javascript");
        //    scriptControl2.Attributes.Add("language", "JavaScript");
        //    scriptControl2.Attributes.Add("src", "/common/ymPrompt-4.0-B-20090302/ymPrompt.js");

        //    this.Header.Controls.Add(link1);
        //    this.Header.Controls.Add(scriptControl2);
        //}
        #endregion
//        private void extNet()
//        {
//            if (!IsPostBack)
//            {
//                if (PageManager.Instance != null)
//                {
//                    HttpCookie themeCookie = Request.Cookies["Theme"];
//                    if (themeCookie != null)
//                    {
//                        string themeValue = themeCookie.Value;
//                        PageManager.Instance.Theme = (Theme)Enum.Parse(typeof(Theme), themeValue, true);
//                    }

//                    HttpCookie langCookie = Request.Cookies["Language"];
//                    if (langCookie != null)
//                    {
//                        string langValue = langCookie.Value;
//                        PageManager.Instance.Language = (Language)Enum.Parse(typeof(Language), langValue, true);
//                    }
//                }
//            }

//#if DEBUG
//            if (!(this is _default) && !(this is source))
//            {
//                LinkButton btnPostback = new LinkButton();
//                btnPostback.Text = "<span title=\"Postback this page without AJAX - only for DEBUG usage.\">&nbsp;&nbsp;&nbsp;&nbsp;</span>";
//                btnPostback.EnableAjax = false;
//                btnPostback.CssStyle = "position: absolute; left: 0px; bottom: 0px; background-color: #EFF6FF;";
//                Page.Form.Controls.Add(btnPostback);
//            }
//#endif


//        }


      
        #region Grid related section
        //protected string HowManyRowsAreSelectedById(Grid grid)
        //{
        //    StringBuilder sb = new StringBuilder();
        //    int selectedCount = grid.SelectedRowIndexArray.Length;
        //    if (selectedCount > 0)
        //    {
        //        for (int i = 0; i < selectedCount; i++)
        //        {
        //            int rowIndex = grid.SelectedRowIndexArray[i];
       
        //            if (grid.AllowPaging && !grid.IsDatabasePaging)
        //            {
        //                rowIndex = grid.PageIndex * grid.PageSize + rowIndex;
        //            }

        //            foreach (object key in grid.DataKeys[rowIndex])
        //            {
        //                //sb.AppendFormat("<td>{0}</td>", key);
        //                sb.Append(key + ",");
        //            }
        //        }
        //    } 
        //    return sb.ToString();
        //}
        //protected string HowManyRowsAreSelected(Grid grid)
        //{
        //    StringBuilder sb = new StringBuilder();
        //    int selectedCount = grid.SelectedRowIndexArray.Length;
        //    if (selectedCount > 0)
        //    {
        //        sb.AppendFormat("Selected rows ({0}): ", selectedCount);
        //        sb.Append("<table class=\"result\"><tbody>");

        //        // Table header
        //        sb.Append("<tr><th>index</th>");
        //        foreach (string datakey in grid.DataKeyNames)
        //        {
        //            sb.AppendFormat("<th>{0}</th>", datakey);
        //        }
        //        sb.Append("</tr>");

        //        for (int i = 0; i < selectedCount; i++)
        //        {
        //            int rowIndex = grid.SelectedRowIndexArray[i];
        //            sb.AppendFormat("<tr><td>{0}</td>", rowIndex);

        //            // If allow paging, not database paging.
        //            if (grid.AllowPaging && !grid.IsDatabasePaging)
        //            {
        //                rowIndex = grid.PageIndex * grid.PageSize + rowIndex;
        //            }

        //            foreach (object key in grid.DataKeys[rowIndex])
        //            {
        //                sb.AppendFormat("<td>{0}</td>", key);
        //            }
        //            sb.Append("</tr>");
        //        }
        //        sb.Append("</tbody></table>");
        //    }
        //    else
        //    {
        //        sb.Append("No row was selected.");
        //    }

        //    return sb.ToString();
        //}

       


        #endregion
        public IWindsorContainer ObtainContainer()
        {

            IContainerAccessor containerAccessor =

            HttpContext.Current.ApplicationInstance as IContainerAccessor;
            if (containerAccessor == null)
            {
                throw new ApplicationException("你必须在HttpApplication中实现接口 IContainerAccessor 暴露容器的属性");
            }

            IWindsorContainer container = containerAccessor.Container;
            if (container == null)
            {
                throw new ApplicationException("HttpApplication 得不到容器的实例");
            }
            return container;
        }


        //public void checkRole()
        //{
        //    SysUsers user = (SysUsers)Session[SYS_USERS_SESSION_KEY];
        //    string filePath = Request.FilePath;
        //    string fileName = VirtualPathUtility.GetFileName(filePath);
        //    fileName = fileName.Replace(VirtualPathUtility.GetExtension(filePath), "");
        //    Type t = this.GetType();
        //    string typeName = t.BaseType.FullName;
        //    Type type = Type.GetType(typeName);

        //    if (null != type)
        //    {
        //        if (null != user && !"login".Equals(fileName) && !"Home".Equals(fileName))
        //        {
        //            ISysPageSrv sysPageSrv = (BJAPP.Component.ContainerAccessorUtil.GetContainer())["sysPageSrv"] as ISysPageSrv;
        //            IList<SysPage> lists = sysPageSrv.queryPageRoleList(user.Oid);

        //            if (lists.Count > 0)
        //            {
        //                bool falg = false;
        //                foreach (SysPage page in lists)
        //                {
        //                    foreach (Attribute attr in type.GetCustomAttributes(true))
        //                    {
        //                        PageAuthenAttribute sa = attr as PageAuthenAttribute;
        //                        if (null != sa)
        //                        {
        //                            if (sa.ModuleId.Equals(page.Code))
        //                                falg = true; break;
        //                        }
        //                    }
        //                }
        //                if (falg)
        //                {
        //             //       Response.Write("<script>alert('权限验证成功');</script>");
        //                }
        //                else
        //                {
        //                    Session.Remove(SYS_USERS_SESSION_KEY);
        //                    Response.Redirect("~/login/login.aspx");
        //                //    Response.Write("<script>alert('没有权限,请重新登录');</script>");
        //                }
        //            }
        //        }
        //    }
        //}
    }
}
